<?php

    namespace App\Models;

    use Illuminate\Notifications\Notifiable;
    use Illuminate\Auth\MustVerifyEmail as MustVerifyEmailTrait;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Illuminate\Contracts\Auth\MustVerifyEmail as MustVerifyEmailContract;
    use Illuminate\Support\Facades\Auth;
    use Spatie\Permission\Traits\HasRoles;

    class User extends Authenticatable implements MustVerifyEmailContract
    {
        use Traits\LastActivedAtHelper;
        use HasRoles;
        use MustVerifyEmailTrait;
        use Traits\ActiveUserHelper;

        use Notifiable {
            notify as protected laravelNotify;
        }

        public function notify($instance)
        {
            // 如果要通知的人是当前用户，就不必通知了！
            if ($this->id == Auth::id()) {
                return;
            }

            // 只有数据库类型通知才需提醒，直接发送 Email 或者其他的都 Pass
            if (method_exists($instance, 'toDatabase')) {
                $this->increment('notification_count');
            }

            $this->laravelNotify($instance);
        }

        protected $fillable = [
            'name', 'email', 'password', 'introduction', 'avatar'
        ];

        protected $hidden = [
            'password', 'remember_token',
        ];

        public function topics()
        {
            return $this->hasMany(Topic::class);
        }

        public function isAuthorOf($model)
        {
            return $this->id == $model->user_id;
        }

        public function replies()
        {
            return $this->hasMany(Reply::class);
        }

        public function markAsRead()
        {
            $this->notification_count = 0;
            $this->save();
            $this->unreadNotifications->markAsRead();
        }

        public function setPasswordAttribute($value)
        {
            // 如果值的长度等于 60，即认为是已经做过加密的情况
            if (strlen($value) != 60) {

                // 不等于 60，做密码加密处理
                $value = bcrypt($value);
            }

            $this->attributes['password'] = $value;
        }

        public function setAvatarAttribute($path)
        {
            // 如果不是 `http` 子串开头，那就是从后台上传的，需要补全 URL
            if ( ! starts_with($path, 'http')) {

                // 拼接完整的 URL
                $path = config('app.url') . "/uploads/images/avatars/$path";
            }

            $this->attributes['avatar'] = $path;
        }
    }